package cn.hy.mygame.entity;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import cn.hy.mygame.global.Config;
import cn.hy.mygame.model.BaseModel;
import cn.hy.mygame.model.TouchAble;
import cn.hy.mygame.view.GameView;

public class EmplacePlant extends BaseModel implements TouchAble {
	private int locationX;
	private int locationY;
	private boolean isAlive;
	private Rect touchArea;
	private PlantType type;
	
	public enum PlantType {
		FLOWER, PEA
	}
	
	public EmplacePlant (int locationX, int locationY, PlantType type) {
		this.locationX = locationX;
		this.locationY = locationY;
		this.type = type;
		touchArea = new Rect(locationX, locationY, locationX + Config.flowerFrames[0].getWidth(), locationY + Config.flowerFrames[0].getHeight());
		isAlive = true;
	}
	
	@Override
	public void drawSelf(Canvas canvas, Paint paint) {
		// TODO Auto-generated method stub
		if (isAlive) {
			if (this.type == PlantType.FLOWER) {
				canvas.drawBitmap(Config.flowerFrames[0], locationX, locationY, paint);
			} else {
				canvas.drawBitmap(Config.PeaFrames[0], locationX, locationY, paint);
			}
		}
		super.drawSelf(canvas, paint);
	}

	public int getLocationX() {
		return locationX;
	}

	public void setLocationX(int locationX) {
		this.locationX = locationX;
	}

	public int getLocationY() {
		return locationY;
	}

	public void setLocationY(int locationY) {
		this.locationY = locationY;
	}

	public boolean isAlive() {
		return isAlive;
	}

	public void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}

	public Rect getTouchArea() {
		return touchArea;
	}

	public void setTouchArea(Rect touchArea) {
		this.touchArea = touchArea;
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		// TODO Auto-generated method stub
		int x = (int) event.getX();
		int y = (int) event.getY();
		if (touchArea.contains(x, y)) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				
				break;
			case MotionEvent.ACTION_MOVE:
				locationX = x - Config.flowerFrames[0].getWidth()/2;
				locationY = y - Config.flowerFrames[0].getHeight()/2;
				touchArea.offsetTo(locationX, locationY);
				break;
			case MotionEvent.ACTION_UP:
				apply4Plant();
				isAlive = false;
				break;
			default:
				break;
			}
			return true;
		}
		return false;
	}

	private void apply4Plant() {
		// TODO Auto-generated method stub
		GameView.getInstance().apply4Plant(locationX, locationY, type);
	}
}
